<?php

/**
 * 命令行：省市区初始化
 */

namespace app\crontab\controller\once;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Region extends Command {
    protected function configure() {
        $this->setName('Region');
    }

    protected function execute(Input $input, Output $output) {
        $this->initials();
    }

    public function initials() {
        $data = db('region')->select();
        $model = new \app\crontab\model\Region;
        foreach ($data as $key => $val) {
            $initials = initials($val['name']);
            $model->edit($initials, $val['id']);
//            db('region')->where(['id' => $val['id']])->update(['initials' => $initials]);
        }
    }

    /**
     * 读取Excel内容
     * @param $fields 字段映射
     * @return array
     */
    public function readExcel() {
        $filePath = "./application/crontab/region.csv";
        $file = fopen($filePath, "r");
        while (!feof($file)) {
            $playerData[] = (fgetcsv($file));
        }
        fclose($file);
        return $this->convertArr($playerData);
    }

    public function convertArr($arr) {
        $data = $provinces = $citys = $areas = $addresss = [];
        $id = 1;
        foreach ($arr as $key => $val) {
            if ($key > 0 && !empty($val[2])) {
                !isset($provinces[$val[1]]) && $provinces[$val[1]] = [
                    'id'        => $id++,
                    'city_code' => $val[1],
                    'pid'       => 0,
                    'pids'      => 0,
                    'p_code'    => 0,
                    'p_codes'   => 0,
                    'name'      => mb_convert_encoding($val[2], "UTF-8", "GBK"),
                    'rank'      => 1,
                ];
                $province = $provinces[$val[1]];
                !isset($citys[$val[3]]) && $citys[$val[3]] = [
                    'id'        => $id++,
                    'city_code' => $val[3],
                    'pid'       => $province['id'],
                    'pids'      => ",{$province['id']},",
                    'p_code'    => $province['city_code'],
                    'p_codes'   => ",{$province['city_code']},",
                    'name'      => mb_convert_encoding($val[4], "UTF-8", "GBK"),
                    'rank'      => 2,
                ];
                $city = $citys[$val[3]];
                !isset($areas[$val[5]]) && $areas[$val[5]] = [
                    'id'        => $id++,
                    'city_code' => $val[5],
                    'pid'       => $city['id'],
                    'pids'      => "{$city['pids']}{$city['id']},",
                    'p_code'    => $city['city_code'],
                    'p_codes'   => "{$city['p_codes']}{$city['city_code']},",
                    'name'      => mb_convert_encoding($val[6], "UTF-8", "GBK"),
                    'rank'      => 3,
                ];
                $area = $areas[$val[5]];
                !isset($addresss[$val[7]]) && $addresss[$val[7]] = [
                    'id'        => $id++,
                    'city_code' => $val[7],
                    'pid'       => $area['id'],
                    'pids'      => "{$area['pids']}{$area['id']},",
                    'p_code'    => $area['city_code'],
                    'p_codes'   => "{$area['p_codes']}{$area['city_code']},",
                    'name'      => mb_convert_encoding($val[8], "UTF-8", "GBK"),
                    'rank'      => 4,
                ];
                $address = $addresss[$val[7]];
                !isset($data[$val[1]]) && $data[$val[1]] = $province;
                !isset($data[$val[3]]) && $data[$val[3]] = $city;
                !isset($data[$val[5]]) && $data[$val[5]] = $area;
                !isset($data[$val[7]]) && $data[$val[7]] = $address;

            }
        }
        $data = array_values($data);
        $num = 0;
        $datas = [];
        foreach ($data as $key => $val) {
            $num++;
            $datas[] = $val;
            if ($num > 400) {
//                print_r($datas);exit;
                $count = db('Region')->insertAll($datas);
                echo $count . "\n";
                $num = 0;
                $datas = [];
            }
            if (!isset($data[$key + 1])) {
//                print_r($datas);exit;
                $count = db('Region')->insertAll($datas);
                echo $count . "\n";
            }
        }

    }
}